package org.enwey.icarenew.Activity.Setting;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.BaseAdapter;
import android.widget.TextView;

import org.enwey.icarenew.Activity.Base.BaseActivity;
import org.enwey.icarenew.Activity.MainActivity;
import org.enwey.icarenew.Manager.SettingsManager;
import org.enwey.icarenew.R;

/**
 * Created by John on 1/28/2015.
 */
public class SettingActivity extends BaseActivity {
    private TextView mLogout;
    private AlertDialog mLogoutDialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        setUp();
        findViews();
        registerListener();
    }
    private void setUp() {
        setTitle("设置");
        showLeftBtn();
    }

    private void findViews() {
        mLogout = (TextView) this.findViewById(R.id.activity_setting_logout);
    }

    private void registerListener() {
        View.OnClickListener register = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                doLogout();
            }
        };
        mLogout.setOnClickListener(register);
    }

    private void doLogout() {
        if (mLogoutDialog != null) {
            mLogoutDialog.dismiss();
        }
        mLogoutDialog = new AlertDialog.Builder(this)
                .setMessage("确定要退出当前账号吗？")
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Intent intent = new Intent();
                        SettingsManager.deleteLoginUser();
                        intent.setClass(SettingActivity.this, MainActivity.class);
                        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        startActivity(intent);
                        finish();
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                })
                .create();
        mLogoutDialog.show();
    }
}
